import {
	getAuthInfo
} from '@/apis/authorization';

export const state = {
	/** 权限代码列表（Permission code list） */
	permCodeList: [],
	/** 后台菜单列表（Backstage menu list） */
	backMenuList: [],
	/** 前台菜单列表（Front menu list） */
	frontMenuList: [],
	/** 最后一次构建菜单的时间（To trigger a menu update） */
	lastBuildMenuTime: 0,
}

/** 获取器 */
export const getters = {
	/** 获取权限代码列表 */
	getPermCodeList: (state, getters, dispatch) => {
		// console.log('getPermCodeList', {state, getters, dispatch, CodeList: state.permCodeList});
		return state.permCodeList;
	},
	/** 获取后台菜单列表 */
	getBackMenuList: state => {
		return state.backMenuList;
	},
	/** 获取前台菜单列表 */
	getFrontMenuList: state => {
		return state.frontMenuList;
	},
	/** 获取最后一次构建菜单的时间 */
	getLastBuildMenuTime: state => {
		return state.lastBuildMenuTime;
	},
}

/** 方法 */
export const actions = {
	/** 设置权限代码列表 */
	setPermCodeList: function({
		state,
		commit,
		dispatch
	}, codeList) {
		// console.log('setPermCodeList_actions', {CodeList: codeList, CodeList: state.permCodeList});
		state.permCodeList = codeList;
	},
	/** 设置后台菜单列表 */
	setBackMenuList: function({
		state,
		commit,
		dispatch
	}, list) {
		state.backMenuList = list;
		if (list && list?.length > 0) {
			commit('setLastBuildMenuTime');
		}
	},
	/** 设置前台菜单列表 */
	setFrontMenuList: function({
		state,
		commit,
		dispatch
	}, list) {
		state.frontMenuList = list;
	},
	/** 设置触发菜单更新时间 */
	setLastBuildMenuTime: function({
		state,
		commit,
		dispatch
	}) {
		state.lastBuildMenuTime = new Date().getTime();
	},
	/** 重置权限状态 */
	resetPermissionState: function({
		state,
		commit,
		dispatch
	}) {
		state.permCodeList = [];
		state.backMenuList = [];
		state.lastBuildMenuTime = 0;
	},
	/** 更改权限代码 */
	changePermissionCode: async function({
		state,
		commit,
		dispatch
	}) {
		const codeList = await getAuthInfo();
		await dispatch('setPermCodeList', codeList);
	},
}
